using System;
using System.Collections.Generic;
using System.Text;

namespace XnaDevRu.BulletX
{
	public class BroadphasePair
	{
		private BroadphaseProxy _proxyA;
		private BroadphaseProxy _proxyB;

		private CollisionAlgorithm _algorithm;
		private object _userInfo;

		public BroadphasePair()
		{
		}

		public BroadphasePair(BroadphasePair other)
		{
			_proxyA = other._proxyA;
			_proxyB = other._proxyB;

			_algorithm = other._algorithm;
			_userInfo = null;
		}

		public BroadphasePair(BroadphaseProxy proxyA, BroadphaseProxy proxyB)
		{
			_proxyA = proxyA;
			_proxyB = proxyB;

			_algorithm = null;
			_userInfo = null;
		}

		public BroadphaseProxy ProxyA { get { return _proxyA; } set { _proxyA = value; } }
		public BroadphaseProxy ProxyB { get { return _proxyB; } set { _proxyB = value; } }

		public CollisionAlgorithm CollisionAlgorithm { get { return _algorithm; } set { _algorithm = value; } }
		public object UserInfo { get { return _userInfo; } set { _userInfo = value; } }

		public override int GetHashCode()
		{
			return _proxyA.GetHashCode() ^ _proxyB.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj is BroadphasePair)
				return this == (BroadphasePair)obj;
			return false;
		}

		public static int ComparisonSort(BroadphasePair a, BroadphasePair b)
		{
			int aAId = a.ProxyA != null ? a.ProxyA.ComparisonID : -1;
			int aBId = a.ProxyB != null ? a.ProxyB.ComparisonID : -1;
			int aCId = a.CollisionAlgorithm != null ? a.CollisionAlgorithm.ComparisonID : -1;
			int bAId = b.ProxyA != null ? b.ProxyA.ComparisonID : -1;
			int bBId = b.ProxyB != null ? b.ProxyB.ComparisonID : -1;
			int bCId = b.CollisionAlgorithm != null ? b.CollisionAlgorithm.ComparisonID : -1;
			
			if	(aAId > bAId ||
				(a.ProxyA == b.ProxyA && aBId > bBId) ||
				(a.ProxyA == b.ProxyA && a.ProxyB == b.ProxyB && aCId > bCId))
				return -1;
			else
				return 1;
		}

		public static bool operator ==(BroadphasePair a, BroadphasePair b)
		{
			if (object.Equals(a, null) && object.Equals(b, null))
				return true;
			if (object.Equals(a, null) || object.Equals(b, null))
				return false;

			return (a.ProxyA == b.ProxyA) && (a.ProxyB == b.ProxyB);
		}

		public static bool operator !=(BroadphasePair a, BroadphasePair b)
		{
			if (object.Equals(a, null) && object.Equals(b, null))
				return true;
			if (object.Equals(a, null) || object.Equals(b, null))
				return false;

			return (a.ProxyA != b.ProxyA) || (a.ProxyB != b.ProxyB);
		}
	}
}
